package tagdb.fasta;

import com.compomics.util.experiment.biology.Protein;
import com.compomics.util.experiment.identification.SequenceFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * This file represents a FASTA database.
 *
 * @author Thilo Muth
 */
public class Database {

    /**
     * The filename of the FASTA-Database
     */
    private String filename;
    /**
     * The sequence factory.
     */
    private SequenceFactory sequenceFactory;
    /**
     * List of all FASTA proteins.
     */
    private List<Protein> proteins;
    /**
     * List of all accession
     */
    private List<String> accessions;
    /**
     * The FASTA file.
     */
    private File file;

    /**
     * Constructor for the FASTA database.
     *
     * @param filename
     * @throws ClassNotFoundException
     * @throws IOException
     * @throws InterruptedException
     */
    public Database(final String filename) throws ClassNotFoundException, IOException, InterruptedException {
        sequenceFactory = SequenceFactory.getInstance();
        this.file = new File(filename);
        sequenceFactory.loadFastaFile(file);
        generateProteinList();
        this.filename = file.getName();
    }

    /**
     * Returns FASTA filename.
     *
     * @return The FASTA filename.
     */
    public String getFilename() {
        return filename;
    }

    /**
     * Returns list of all accessions.
     *
     * @return The FASTA accessions.
     */
    public List<String> getAccessions() {
        return accessions;
    }

    /**
     * Returns the demanded protein via accession.
     *
     * @param accession The protein accession.
     * @return Protein protein
     * @throws IOException
     * @throws InterruptedException
     */
    public Protein getProtein(String accession) throws IOException, InterruptedException {
        return sequenceFactory.getProtein(accession);
    }

    /**
     * Generate the protein list. Note that the order of the protein is not(!)
     * the same as in the FASTA file.
     *
     * @throws IOException
     * @throws InterruptedException
     */
    private void generateProteinList() throws IOException, InterruptedException {
        proteins = new ArrayList<Protein>();
        accessions = sequenceFactory.getAccessions();

        // Iterate the accessions.
        for (String accession : getAccessions()) {
            proteins.add(getProtein(accession));
        }
    }

    /**
     * Returns list of all proteins. Note that the order of the protein is
     * not(!) the same as in the FASTA file.
     *
     * @return list of all proteins
     * @throws IOException
     * @throws InterruptedException
     */
    public List<Protein> getProteins() throws IOException, InterruptedException {
        // Lazy loading.
        if (proteins == null) {
            generateProteinList();
        }
        return proteins;
    }
}
